package com.my.schedule.feign;

import com.my.apis.schedule.IScheduleClient;
import com.my.model.common.dtos.ResponseResult;
import com.my.model.schedule.dtos.Task;
import com.my.schedule.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ScheduleClient implements IScheduleClient {
    @Autowired
    private TaskService taskService;

    /**
     * 添加任务
     * @param task   任务对象
     * @return       任务id
     */
    @Override
    @PostMapping("/api/v1/task/add")
    public ResponseResult addTask(@RequestBody Task task) {
        return ResponseResult.okResult(taskService.addTask(task));
    }

    /**
     * 取消任务
     * @param taskId        任务id
     * @return              取消结果
     */
    @Override
    @GetMapping("/api/v1/task/cancel/{taskId}")
    public ResponseResult cancelTask(@PathVariable long taskId) {
        return ResponseResult.okResult(taskService.cancelTask(taskId));
    }


    /**
     * 按照类型和优先级来拉取任务
     * @param type
     * @param priority
     * @return
     */
    @Override
    @GetMapping("/api/v1/task/poll/{type}/{priority}")
    public ResponseResult poll(@PathVariable("type") int type,@PathVariable("priority") int priority) {
        return ResponseResult.okResult(taskService.poll(type,priority));
    }

    /**
     * 删除数据库中任务，更新日志
     * @param taskId
     */
    @Override
    @DeleteMapping("/api/v1/task/delete/{taskId}/{status}")
    public void updateDb(@PathVariable("taskId")Long taskId, @PathVariable("status") Integer status) {
        taskService.UpdateDb(taskId,status);
    }
}
